<template>
    <div :style="carStyle">
        <div v-if="!showContent" class="defaultText">请在右侧配置数据</div>
        <template v-else>
            <div class="lineTitle" v-if="obj.isShowTitle" :style="titleStyle">{{ obj.title }}</div>
        </template>
        <div class="chart" ref="constat" :style="canvasStyle" @click="gotoUrl(obj)"></div>
    </div>
</template>

<script>
import * as echarts from 'echarts';
import 'echarts-liquidfill'
// import { oneLoad, boardOneLoad } from '@/api/share'

export default {
    Name: "LiquidFill",
    components: {
        echarts
    },
    props: {
        obj: Object,
        isReload: {
            type: Boolean,
            default: false
        },
        isEdit: {
            type: Boolean,
            default: false,
        },
        gotoUrl: Function,
        getTableValue: Function,
    },
    data() {
        return {
            showContent: false,
            carStyle: {},
            titleStyle: {},
            canvasStyle: {
                width: "100%",
                height: "100%"
            },
            chart: undefined,
            // 用于比对数据是否修改
            dataStr: "",
            timer: null,
            filterForm: {}
        }
    },
    mounted() {
        this.setStyle();

        this.$nextTick(function () {
            this.reloadInfo();
        })

        this.dataStr = JSON.stringify(this.obj.tableData);

        var self = this;
        window.addEventListener('resize', function () {
            if (self.chart) {
                self.chart.resize();
            }
        }, true);
    },
    beforeDestroy() {
        if (!this.chart) {
            return
        }
        this.chart.dispose()
        this.chart = null
        clearInterval(this.timer)
    },
    methods: {
        reloadInfo() {
            if (this.chart) {
                this.chart.dispose()
            }
            var constat = this.$refs.constat;
            this.chart = echarts.init(constat);

            var timeSize = 60;
            if (this.obj.tableData.reloadTime) {
                timeSize = this.obj.tableData.reloadTime;
            }

            if (this.obj.tableData.type == 1) {
                // 刷新时间兼容处理
                if (timeSize < 60) {
                    timeSize = 60;
                }
            }

            clearInterval(this.timer);
            this.getFirstData();

            if (this.isReload) {
                this.timer = setInterval(() => {
                    this.getLineData();
                }, timeSize * 1000);
            }
        },
        setStyle() {
            var style = this.obj.style;

            var backgroundColor = "";
            var backgroundImage = "";
            // 背景颜色
            if (style.backgroundImage.length) {
                backgroundImage = `url(${style.backgroundImage}`;
            } else if (style.backgroundColor && style.backgroundColor.length > 0) {
                backgroundColor = style.backgroundColor;
            } else {
                backgroundColor = "";
            }

            var boxShadow = "";
            if (style.boxShadow) {
                boxShadow = `${style.boxShadow.h}px ${style.boxShadow.v}px ${style.boxShadow.blur}px ${style.boxShadow.spreed}px ${style.boxShadow.color}`
            }

            this.carStyle = {
                boxShadow,
                width: "100%",
                height: "100%",
                display: "flex",
                backgroundSize: "100% 100%",
                borderStyle: "solid",
                backgroundImage,
                backgroundColor,
                color: style.color,
                fontSize: style.fontSize + "px",
                justifyContent: style.justifyContent,
                alignItems: style.alignItems,
                borderColor: style.borderColor,
                borderWidth: style.borderWidth + "px",
                borderRadius: style.borderRadius + "px",
                fontWeight: style.fontWeight,
                fontStyle: style.fontStyle,
            }

            // #region 新的背景颜色

            if (this.obj.style.bgcolorType == "image") {
                this.carStyle.backgroundImage = backgroundImage;
            } else if (this.obj.style.bgcolorType == "gradient") {
                if (this.obj.style.gradient.isRadial) {
                    this.carStyle.backgroundImage = `radial-gradient(${this.obj.style.gradient.colorStart}, ${this.obj.style.gradient.colorEnd})`;
                } else {
                    this.carStyle.backgroundImage = `linear-gradient(${this.obj.style.gradient.fontAngle}deg, ${this.obj.style.gradient.colorStart}, ${this.obj.style.gradient.colorEnd})`;
                }
            } else {
                this.carStyle.backgroundColor = backgroundColor;
            }

            // #endregion

            this.titleStyle = {
                justifyContent: style.justifyContent,
                alignItems: style.alignItems,
            }

            // 标题隐藏显示
            var isShowTitle = this.obj.isShowTitle ? true : false;
            var echarHeight = style.height;
            var echarMarginTop = 0;
            if (isShowTitle) {
                echarHeight = style.height - style.fontSize - style.borderWidth * 2;
                echarMarginTop = style.fontSize + "px";
            }

            this.canvasStyle = {
                marginTop: echarMarginTop,
                width: "100%",
                height: echarHeight + "px",
            }

            if (this.chart) {
                this.chart.resize({
                    width: style.width,
                    height: echarHeight,
                });
            }
        },
        setOptions(text, data) {
            var textStyle = {
                fontSize: '52px', //文本字号,
                color: "#fff",
                fontWeight: '',
                fontStyle: ''
            }

            if (this.obj.tableData.fontSize) {
                textStyle.fontSize = this.obj.tableData.fontSize + "px";
            }
            if (this.obj.tableData.fontColor) {
                textStyle.color = this.obj.tableData.fontColor;
            }
            if (this.obj.tableData.fontWeight) {
                textStyle.fontWeight = this.obj.tableData.fontWeight;
            }
            if (this.obj.tableData.fontStyle) {
                textStyle.fontStyle = this.obj.tableData.fontStyle;
            }

            this.chart.setOption({
                // backgroundColor: , //背景色
                series: [
                    {
                        type: 'liquidFill', //水位图
                        radius: '90%', //显示比例
                        center: ['50%', '50%'], //中心点
                        amplitude: '5%', //水波振幅
                        data: [data, data, data], // data个数代表波浪数
                        color: [
                            {
                                type: 'linear',
                                x: 0,
                                y: 0,
                                x2: 0,
                                y2: 1,
                                colorStops: [
                                    {
                                        offset: 0,
                                        color: this.obj.tableData.rateBackageColor,
                                    },
                                    // {
                                    //     offset: 1,
                                    //     color: '#2ca3e2',
                                    // },
                                ],
                                globalCoord: false,
                            },
                        ], //波浪颜色
                        backgroundStyle: {
                            borderWidth: 1, //外边框
                            // borderColor: '#23cc72', //边框颜色
                            color: this.obj.tableData.valueColor, //边框内部填充部分颜色
                        },
                        label: {
                            //标签设置
                            position: ['50%', '50%'],
                            formatter: text, //显示文本,
                            textStyle: textStyle,
                        },
                        outline: {
                            // show: false
                            borderDistance: 0,
                            itemStyle: {
                                borderWidth: 1,
                                borderColor: this.obj.tableData.rateBackageColor,
                            },
                        },
                    },
                ],
                // title: {
                //     text: text,
                //     x: 'center',
                //     y: 'center',
                //     textStyle: {
                //         fontSize: 28,
                //         color: '#333333'
                //     }
                // },
                // tooltip: {
                //     trigger: 'item',
                //     show: false
                // },
                // series: [{
                //     type: 'pie',
                //     center: ['50%', '50%'],
                //     radius: ['45%', '55%'],
                //     clockwise: false,
                //     avoidLabelOverlap: false,
                //     hoverOffset: 0,
                //     itemStyle: {
                //         normal: {
                //             color: function (params) {
                //                 return colorList[params.dataIndex]
                //             }
                //         }
                //     },
                //     label: {
                //         show: false,
                //     },
                //     labelLine: {
                //         normal: {
                //             length: 20,
                //             length2: 30,
                //             lineStyle: {
                //                 width: 1
                //             }
                //         }
                //     },
                //     roundCap: true,
                //     data: data,
                // }]
            })
        },
        getFirstData() {
            if (this.obj.tableData.tableId && this.obj.tableData.itemId && this.obj.tableData.maxNumber > 0) {
                var result = this.getTableValue(this.obj.tableData.tableId, this.obj.tableData.itemId, 1);
                if (result.code == 200) {
                    this.getLineData();
                } else {
                    var self = this;
                    window.setTimeout(function () {
                        self.getFirstData();
                    }, 1000);
                }
            } else {
                this.getLineData();
            }
        },
        getLineData() {
            var maxValue = this.obj.tableData.maxNumber;
            // #region 没有配置数据显示默认内容
            if (!this.obj.tableData.tableId) {

                this.showContent = true;
                var value = 60;
                var text = "100%";
                var data = 100;
                if (maxValue > 0) {
                    if (this.obj.tableData.decimal >= 0) {
                        text = Number(value / maxValue * 100).toFixed(this.obj.tableData.decimal) + "%";
                    } else {
                        text = Math.floor(value / maxValue * 100) + "%";
                    }
                    data = (value / maxValue).toFixed(2);
                }

                this.setOptions(text, data);
                return false;
            }
            // #endregion

            // #region 参数不完整
            if (!this.obj.tableData.itemId || this.obj.tableData.maxNumber <= 0) {
                this.showContent = false;
                return false;
            }
            this.showContent = true;
            var result = this.getTableValue(this.obj.tableData.tableId, this.obj.tableData.itemId, 1);
            if (result.code == 200) {
                var value = result.data;
                var text = "100%";
                var data = 100;
                if (maxValue > 0) {
                    if (value != null) {
                        if (this.obj.tableData.decimal >= 0) {
                            text = Number(value / maxValue * 100).toFixed(this.obj.tableData.decimal) + "%";
                        } else {
                            text = Math.floor(value / maxValue * 100) + "%";
                        }
                        data = (value / maxValue).toFixed(2);
                    } else {
                        text = "0%";
                        data = 0;
                    }
                }
                this.setOptions(text, data);
            }
            return false;
            // } else {
            this.filterForm.tableId = this.obj.tableData.tableId;
            this.filterForm.structureId = this.obj.tableData.itemId;

            if (this.obj.tableData.type != 1) {
                oneLoad(this.filterForm).then((res) => {
                    if (res.data.code == 0) {

                        var value = res.data.data;

                        this.showContent = true;

                        var text = "100%";
                        var data = 100;
                        if (maxValue > 0) {
                            if (value != null) {
                                if (this.obj.tableData.decimal >= 0) {
                                    text = Number(value / maxValue * 100).toFixed(this.obj.tableData.decimal) + "%";
                                } else {
                                    text = Math.floor(value / maxValue * 100) + "%";
                                }
                                data = (value / maxValue).toFixed(2);
                            } else {
                                text = "0%";
                                data = 0;
                            }
                        }

                        this.setOptions(text, data);
                    } else {
                        console.error(res.data.message)
                    }
                }).catch(() => {
                    clearInterval(this.timer)
                });
            } else {
                boardOneLoad(this.filterForm).then((res) => {
                    if (res.data.code == 0) {
                        var maxValue = this.obj.tableData.maxNumber;
                        var value = res.data.data;

                        this.showContent = true;

                        var text = "100%";
                        var data = 100;
                        if (maxValue > 0) {
                            if (value > 0) {
                                text = Math.floor(value / maxValue * 100) + "%";
                                data = (value / maxValue).toFixed(2);
                            } else {
                                text = "0%";
                                data = 0;
                            }
                        }

                        this.setOptions(text, data);
                    } else {
                        console.error(res.data.message)
                    }
                }).catch(() => {
                    clearInterval(this.timer)
                });
            }
            // }
        }
    },
    watch: {
        obj: {
            handler(newVal, oldVal) {
                this.setStyle();

                // 数据改变了 动态更新echart
                if (JSON.stringify(this.obj.tableData) != this.dataStr) {
                    this.dataStr = JSON.stringify(this.obj.tableData);

                    this.reloadInfo();
                }
            },
            deep: true,  // 深度监听
        }
    },
}
</script>